//
//  BYAddImageViewImageCollectionViewCell.m
//  ZhaoQiPei
//
//  Created by 找汽配 on 16/2/29.
//  Copyright © 2016年 祥运. All rights reserved.
//

#import "XYImageSelectCollectionViewCell.h"
#import "XYCircularProgressView.h"
#import "XYImageSelectModel.h"

@interface XYImageSelectCollectionViewCell () <XYImageSelectModelDelegate>

@property (weak, nonatomic) IBOutlet UIImageView * imageView;
@property (weak, nonatomic) IBOutlet UIButton * selectButton;
@property (weak, nonatomic) IBOutlet UIButton * deleteButton;
@property (weak, nonatomic) IBOutlet XYCircularProgressView * progressView;

@property (nonatomic, weak) XYImageSelectModel * model;

@end

@implementation XYImageSelectCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [self.progressView setRadius:20 lineWidth:5];
}

- (void)reloadData:(id)entity {
    self.model = entity;
    self.model.delegate = self;
    
    [self reloadCell];
}

- (void)reloadCell {
    if (self.model.type == XYImageSelectTypeImage) {
        self.selectButton.hidden = YES;
        self.deleteButton.hidden = !self.isEditEnabled;
        self.imageView.hidden = NO;
        self.imageView.image = self.model.image;
        if (!self.model.image) {
            [self.model reloadImage];
        }

        if (self.model.isUploading) {
            self.progressView.hidden = NO;
        }
        else {
            self.progressView.hidden = YES;
        }
        self.progressView.progress = self.model.progress;
    }
    else if (self.model.type == XYImageSelectTypeSelect) {
        self.selectButton.hidden = NO;
        self.deleteButton.hidden = YES;
        self.imageView.hidden = YES;
        self.progressView.hidden = YES;
    }
}

#pragma mark - Button Action

- (IBAction)deleteButtonTouchUpInside {
    if (self.delegate && [self.delegate respondsToSelector:@selector(imageSelectCollectionViewCellShouldDeleteImageWithImageSelectModel:)]) {
        [self.delegate imageSelectCollectionViewCellShouldDeleteImageWithImageSelectModel:self.model];
    }
}

- (IBAction)selectButtonTouchUpInside {
    if (self.delegate && [self.delegate respondsToSelector:@selector(imageSelectCollectionViewCellShouldSelectImage)]) {
        [self.delegate imageSelectCollectionViewCellShouldSelectImage];
    }
}

#pragma mark - Setter

- (void)setDeleteImage:(UIImage *)deleteImage {
    [self.deleteButton setBackgroundImage:deleteImage forState:UIControlStateNormal];
}

- (void)setSelectImage:(UIImage *)selectImage {
    [self.selectButton setBackgroundImage:selectImage forState:UIControlStateNormal];
}

- (void)setEditEnabled:(BOOL)editEnabled {
    _editEnabled = editEnabled;
    [self reloadCell];
}

#pragma mark - XYImageSelectModelDelegate

- (void)imageSelectModelDidUpdate {
    [self reloadCell];
}

- (void)imageSelectModelWillUploadImage {
    self.progressView.hidden = NO;
}

- (void)imageSelectModelDidUploadImage {
    self.progressView.hidden = YES;
    self.progressView.progress = 0;
}

- (void)imageSelectModelUploadingImageWithProgress:(CGFloat)progress {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.progressView.progress = progress;
    });
}

@end
